package com.itheima.jobs;

import com.itheima.constant.RedisConstant;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.Set;

/**
 * 定义Job，实现定时清理
 */
public class ClearImgJob {
    @Autowired
    private JedisPool jedisPool;
    public void clearImg(){
        //跟我Redis中保存的俩个set集合进行计算差值获取垃圾图片

        Set<String> set = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
            if(set != null){
                for (String picName : set) {
                    //删除图片
                    QiniuUtils.deleteFileFromQiniu(picName);
                    //删除set集合中的图片名字
                    jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,picName);
                }
            }
        System.out.println("删除成功");
        }
}
